/**  
 * All rights Reserved, Designed By www.tydic.com
 * @Title:  MarketServiceImpl.java   
 * @Package com.axin.service.impl   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: axin     
 * @date:   2019年3月11日 下午10:22:07   
 * @version V1.0 
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */
package com.axin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.axin.common.entity.ApiToken;
import com.axin.common.entity.JsonResult;
import com.axin.common.entity.response.Kline;
import com.axin.common.entity.response.KlineResponse;
import com.axin.common.entity.response.Merged;
import com.axin.common.entity.response.MergedResponse;
import com.axin.common.exception.ApiException;
import com.axin.common.huobi.HttpRequest;
import com.axin.service.MarketService;
import com.fasterxml.jackson.core.type.TypeReference;

/**   
 * @Description: 市场行情 
 * @ClassName:  MarketServiceImpl
 * @author:  Axin 
 * @date:   2019年3月11日 下午10:22:07   
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */

@Service
public class MarketServiceImpl implements MarketService{

	
	@Autowired
	private HttpRequest httpRequest;
	
	/**K线请求url*/
	@Value("${API_MARKET_HISTORY_KLINE}")
	private String API_MARKET_HISTORY_KLINE;
	/**获取聚合行情*/
	@Value("${API_MARKET_DETAIL_MERGED}")
	private String API_MARKET_DETAIL_MERGED;
	
	
	@Override
	public JsonResult getKline(ApiToken token, Map<String,String> param) {
        KlineResponse resp = httpRequest.get(token,API_MARKET_HISTORY_KLINE, param, new TypeReference<KlineResponse<List<Kline>>>(){});
        if(!"ok".equals(resp.getStatus())){
        	throw new RuntimeException("接口请求异常！");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("list", resp.data);
        map.put("symbol", param.get("symbol"));
        map.put("period", param.get("period"));
        return new JsonResult(map);
	}


	/* (non-Javadoc)
	 * @see com.axin.service.MarketService#getDetailMerged(com.axin.common.entity.ApiToken, java.util.Map)
	 */
	@Override
	public Merged getDetailMerged(ApiToken token, Map<String, String> param) throws ApiException,Exception {
		
		MergedResponse<Merged> resp = httpRequest.get(token,API_MARKET_DETAIL_MERGED, param, new TypeReference<MergedResponse<Merged>>() {
	    });
		Merged data = resp.checkAndReturn();
		return data;
		
	}


	/* (non-Javadoc)
	 * @see com.axin.service.MarketService#getDetailMerged(com.axin.common.entity.ApiToken, java.lang.String)
	 */
	@Override
	public Merged getDetailMerged(ApiToken token, String symbol) throws ApiException, Exception {
		 Map<String,String> param = new HashMap();
	     param.put("symbol", symbol);
	     return getDetailMerged(token, param);
	}
	
	

	
	
	
}
